<?php
namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\DepartmentVali;
use App\Model\Backstage\Department;
use App\Model\Backstage\User;
use App\Tool\Error;
use Illuminate\Http\Request;
use Validator;

/**
 * Class DepartmentController
 * @package App\Http\Controllers\Backstage
 * 公司对应部门控制器
 */
class DepartmentController extends Controller
{
    protected $department;
    public function __construct()
    {
        $this->department = new Department();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取部门列表，如果传公司ID 获取本公司部门
     */
    public function getDepartmentList (Request $request)
    {
        $map['is_del'] = 0;
        $name = $request->input('name'); //部门名称检索
        isset($name)?$map[] = ['name', 'like', "%".$name."%"]:'';
        $list = $this->department->getDepartmentList($map);
        if (!$list){
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok',$list);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 添加部门信息
     */
    public function addDepartment(Request $request)
    {
        $params = $request->except('token');
        $vali = DepartmentVali::validate($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->department->addDepartment($params);
        if($res){
            return $this->success('ok','添加成功！');
        }else{
            return $this->error(Error::ADD_ERROR);
        }

    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 修改部门信息
     */
    public function editDepartment (Request $request)
    {
        if (!$request->has('id')){
            return $this->error(Error::PARAM_MISSING);
        }
        $params = $request->except('token');
        $vali = DepartmentVali::validate($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->department->saveDepartment($params);
        if($res){
            return $this->success('ok','修改成功！');
        }else{
            return $this->error(Error::EDIT_ERROR);
        }
    }


    /**
     * @param Request $request
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     * 删除部门信息 逻辑删除  需要判断部门下面是否有用户
     */
    public function delDepartment (Request $request)
    {
        $id = $request->input('id');
        if(!$id){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->department->delDepartment($id);
        if($res['code'] == 0){
            return $this->success('ok','删除成功！');
        }else{
            return $this->error(1,$res['msg']);
        }
    }

    /**
     * 获取中介公司下所有部门下拉框
     */
    public function dropdown()
    {
        $map['is_del'] = 0;
        $list = $this->department->getDropdown($map);
        if (!$list){
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok',$list);
    }

    /**
     * 根据部门的ID   获取该部门下面的员工下拉框内容
     */
    public function userDropdown(Request $request)
    {
        $map = [];
        $dId = $request->input('d_id');
        if($dId != 0) {
            $map['u_d_id'] = $dId;
        }
        $list  = $this->department->downList($map);
        if (sizeof($list) == 0) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }




}